package com.itheima.mapper;

import com.itheima.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentMapper {
    // 查询所有
    public List selectAll(){
        InputStream is = null;
        SqlSession sqlSession = null;
        List<Student> list = null;
        try {
            // 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取sqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过sqlSession工厂对象获取sqlSession对象
            sqlSession = sqlSessionFactory.openSession();
            // 执行映射配置文件的sql语句
            list = sqlSession.selectList("StudentMapper.selectAll");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    // 添加
    public void insert(Student stu){
        InputStream is = null;
        SqlSession sqlSession = null;
        try {
            // 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过工厂对象获取对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 执行映射配置文件中的sql语句
            int i = sqlSession.insert("StudentMapper.insert",stu);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 删除
    public void delete(Integer sid){
        InputStream is = null;
        SqlSession sqlSession = null;
        try {
            // 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过工厂对象获取对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 执行映射配置文件中的sql语句
            int i = sqlSession.delete("StudentMapper.delete",sid);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public Student findByid(Integer sid) {
        InputStream is = null;
        SqlSession sqlSession = null;
        Student  stu = null;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
            stu = sqlSession.selectOne("StudentMapper.findByid",sid);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stu;
    }

    // 修改
    public void update(Student stu){
        InputStream is = null;
        SqlSession sqlSession = null;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
            sqlSession.update("StudentMapper.update",stu);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
